package com.jdy.nettle.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdy.entity.Data;
import com.jdy.nettle.mapper.DataMapper;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataService extends ServiceImpl<DataMapper, Data> {

    public void start() {
        List<Data> list = new ArrayList<>();
        String file = "E:\\Temp\\0519\\DS31032B-A-A-333-003-4-39-75_2020.05.13_18_20_29.dat";
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String cureent, pre = null;
            Data map = null;
            while ((cureent = in.readLine()) != null) {
                String[] split = cureent.split(",");
                if (cureent.startsWith("EFL")) {
                    map = new Data();
                    map.setEFL(split[1]);
                }

                if (cureent.startsWith("FFL")) {
                    if (map == null) map = new Data();
                    map.setFFL(split[1]);
                }

                if (cureent.startsWith("Step1")) {
//                    if (map == null) map = new HashMap<>();
//                    Map<String, Object> stepMap = new HashMap<>();
                    String key = null;
                    for (int i = 2; i < split.length; i++) {
                        if (pre == null) {
                            key = "" + i;
                        } else if (pre.startsWith("Sag")) {
                            key = "S" + (i - 1);
                            Method method = map.getClass().getMethod("set" + key, String.class);
                            method.invoke(map, split[i]);
                        } else if (pre.startsWith("Tan")) {
                            key = "T" + i;
                        }
//                        stepMap.put(key, split[i]);
                    }
                    baseMapper.insert(map);
//                    if (pre == null) {
//                        System.out.println(list);
//                    } else if (pre.startsWith("Sag")) {
//                        map.put("Sag", stepMap);
//                    } else if (pre.startsWith("Tan")) {
//                        map.put("Tan", stepMap);
//                        list.add(map);
//                    }
                }
                pre = cureent;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
